%===================================================================================================
%[]FUNCTION NAME: CalendarDate.m
%[]AUTHOR: Jinsung Lee
%[]CREATED: 05/03/2016
%[]REVISED: 05/03/2016
%===================================================================================================
%[]FUNCTION DESCRIPTION:
%This function calculates the equivalent Gregorian coordinated universal time of a Julian Day.
%===================================================================================================
%[]INPUT VARIABLES:
%(JD)|Julian Day.
%===================================================================================================
%[]OUTPUT VARIABLES:
%(UTC)|Equivalent Gregorian coordinated universal time.
%===================================================================================================
%[]VARIABLE FORMAT:
%(JD)|Scalar {1 x 1}.
%---------------------------------------------------------------------------------------------------
%(UTC)|Row Vector {1 x 6}.
%===================================================================================================
%[]AUXILIARY FUNCTIONS:
%None.
%===================================================================================================
%[]COMMENTS:
%The equivalent Gregorian coordinated universal time is calculated to the exact second using the
%Jean Meeus algorithm published in "Astronomical Formulas for Calculators".
%===================================================================================================
function UTC = CalendarDate(JD)
    
    Z = fix(JD + 0.5);
    %[]Integer calculation.
    
    F = JD + 0.5 - Z;
    %[]Fractional calculation.
    
    if Z >= 2299161;
        
        alpha = fix((Z - 1867216.25) / 36524.25);
        %[]Integer calculation.
        
        A = Z + 1 + alpha - fix(alpha / 4);
        %[]Integer calculation.
        
    else
        
        A = Z;
        %[]Integer calculation.
        
    end
    
    B = A + 1524;
    %[]Integer calculation.
    
    C = fix((B - 122.1) / 365.25);
    %[]Integer calculation.
    
    D = fix(365.25 * C);
    %[]Integer calculation.
    
    E = fix((B - D) / 30.6001);
    %[]Integer calculation.
    
    Day = B - D - fix(30.6001 * E) + F;
    %[]Current decimal day.
    
    if E > 13.5;
        
        Month = E - 13;
        %[]Current month.
        
    else
        
        Month = E - 1;
        %[]Current month.
        
    end
    
    if Month > 2.5;
        
        Year = C - 4716;
        %[]Current year.
        
    else
        
        Year = C - 4715;
        %[]Current year.
        
    end
    
    Hour = (Day - fix(Day)) * 24;
    %[]Current decimal hour.
    
    Minute = (Hour - fix(Hour)) * 60;
    %[]Current decimal minute.
    
    Second = (Minute - fix(Minute)) * 60;
    %[]Current decimal second.
    
    UTC = [Year,Month,fix(Day),fix(Hour),fix(Minute),Second];
    %[]Equivalent coordinated universal time.
    
end
%===================================================================================================